home *** CD-ROM | disk | FTP | other *** search
- package com.ibm.bsf.engines.netrexx;
-
- import COM.ibm.netrexx.process.NetRexxC;
- import com.ibm.bsf.BSFDeclaredBean;
- import com.ibm.bsf.BSFException;
- import com.ibm.bsf.BSFManager;
- import com.ibm.bsf.util.BSFEngineImpl;
- import com.ibm.bsf.util.BSFFunctions;
- import com.ibm.bsf.util.EngineUtils;
- import com.ibm.cs.util.JavaUtils;
- import com.ibm.cs.util.ReflectionUtils;
- import com.ibm.cs.util.StringUtils;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Hashtable;
- import java.util.Vector;
- import netrexx.lang.BadArgumentException;
- import netrexx.lang.Rexx;
-
- public class NetRexxEngine extends BSFEngineImpl {
- BSFFunctions mgrfuncs;
- Class rexxclass;
- private boolean bsfHandleCreated = false;
- static Hashtable codeToClass = new Hashtable();
- static String serializeCompilation = "";
- static String placeholder = "$$CLASSNAME$$";
- String minorPrefix;
- private int uniqueFileOffset;
-
- public NetRexxEngine() {
- new BadArgumentException();
- }
-
- public void initialize(BSFManager var1, String var2, Vector var3) throws BSFException {
- super.initialize(var1, var2, var3);
- this.mgrfuncs = new BSFFunctions(var1, this);
- }
-
- private GeneratedFile openUniqueFile(String var1, String var2, String var3) {
- File var4 = null;
- Object var5 = null;
- FileOutputStream var6 = null;
- short var7 = 1000;
- GeneratedFile var8 = null;
- String var10 = null;
- int var9 = var7;
- ++this.uniqueFileOffset;
-
- while(var6 == null && var9 > 0) {
- try {
- var10 = var2 + this.uniqueFileOffset;
- var4 = new File(var1 + File.separatorChar + var10 + var3);
- File var13 = new File(var1 + File.separatorChar + var10 + ".class");
- if (var4 != null && !var4.exists() & var13 != null & !var13.exists()) {
- var6 = new FileOutputStream(var4);
- }
- } catch (Exception var12) {
- if (!var4.exists()) {
- System.err.println("openUniqueFile: unexpected " + var12);
- ((Throwable)var12).printStackTrace();
- }
- }
-
- --var9;
- ++this.uniqueFileOffset;
- }
-
- if (var6 == null) {
- System.err.println("openUniqueFile: Failed " + var7 + "attempts.");
- } else {
- var8 = new GeneratedFile(this, var4, var6, var10);
- }
-
- return var8;
- }
-
- public Object eval(String var1, int var2, int var3, Object var4) throws BSFException {
- Object var5 = null;
- String var6 = null;
- GeneratedFile var7 = null;
- String var8 = var4.toString();
- String var9 = var8;
-
- try {
- this.rexxclass = (Class)codeToClass.get(var8);
- if (this.rexxclass != null) {
- if (super.debug) {
- System.err.println("NetRexxEngine: Found pre-compiled class for script '" + var8 + "'");
- }
-
- var6 = this.rexxclass.getName();
- } else {
- var7 = this.openUniqueFile(super.tempDir, "BSFNetRexx", ".nrx");
- if (var7 == null) {
- throw new BSFException("couldn't create NetRexx scratchfile");
- }
-
- var6 = var7.className;
- var7.fos.write(("class " + var6 + ";\n").getBytes());
- var7.fos.write("method BSFNetRexxEngineEntry(bsf=com.ibm.bsf.util.BSFFunctions) public static;\n".getBytes());
- int var12;
- if ((var12 = var9.indexOf(placeholder)) >= 0) {
- for(StringBuffer var14 = new StringBuffer(); var12 >= 0; var12 = var9.indexOf(placeholder, var12)) {
- var14.setLength(0);
- if (var12 > 0) {
- var14.append(var9.substring(0, var12));
- }
-
- var14.append(var6);
- int var38 = var12 + placeholder.length();
- if (var38 < var9.length()) {
- var14.append(var9.substring(var38));
- }
-
- var9 = var14.toString();
- }
- }
-
- for(int var16 = 0; var16 < super.declaredBeans.size(); ++var16) {
- BSFDeclaredBean var40 = (BSFDeclaredBean)super.declaredBeans.elementAt(var16);
- String var15 = StringUtils.getClassName(var40.type);
- var7.fos.write((var40.name + " =" + var15 + " bsf.lookupBean(\"" + var40.name + "\");").getBytes());
- }
-
- var7.fos.write(var9.getBytes());
- var7.fos.close();
- if (super.debug) {
- System.err.println("NetRexxEngine: wrote temp file " + var7.file.getPath() + ", now compiling");
- }
-
- String var17 = var7.file.getPath();
- if (super.debug) {
- var17 = var17 + " -verbose4";
- } else {
- var17 = var17 + " -noverbose";
- }
-
- var17 = var17 + " -nocompile";
- var17 = var17 + " -noconsole";
- Rexx var18 = new Rexx(var17);
- String var19 = serializeCompilation;
- synchronized(var19){}
-
- try {
- NetRexxC.main(var18);
- String var21 = super.tempDir + File.separatorChar + var6 + ".java";
- if (super.debug) {
- System.err.println("NetRexxEngine: compiling Java file " + var21);
- }
-
- if (!JavaUtils.JDKcompile(var21, super.classPath)) {
- throw new BSFException(BSFException.REASON_OTHER_ERROR, "java compilation error");
- }
- } catch (Throwable var28) {
- throw var28;
- }
-
- if (super.debug) {
- System.err.println("NetRexxEngine: loading class " + var6);
- }
-
- this.rexxclass = EngineUtils.loadClass(super.mgr, var6);
- codeToClass.put(var8, this.rexxclass);
- }
-
- Object[] var32 = new Object[]{this.mgrfuncs};
- var5 = this.call((Object)null, "BSFNetRexxEngineEntry", var32);
- } catch (Exception var29) {
- ((Throwable)var29).printStackTrace();
- if (var29 instanceof InvocationTargetException) {
- Throwable var13 = ((InvocationTargetException)var29).getTargetException();
- var13.printStackTrace();
- }
-
- throw new BSFException(BSFException.REASON_IO_ERROR, ((Throwable)var29).getMessage(), var29);
- } finally {
- if (var7 != null && var7.file != null && var7.file.exists()) {
- var7.file.delete();
- }
-
- if (var6 != null) {
- File var33 = new File(super.tempDir + File.separatorChar + var6 + ".java");
- if (var33.exists()) {
- var33.delete();
- }
-
- var33 = new File(super.tempDir + File.separatorChar + var6 + ".class");
- if (var33.exists()) {
- var33.delete();
- }
-
- var33 = new File(super.tempDir + File.separatorChar + var6 + ".crossref");
- if (var33.exists()) {
- var33.delete();
- }
-
- var33 = new File(super.tempDir);
- this.minorPrefix = var6 + "$";
- String[] var39 = var33.list(new 1(this));
- if (var39 != null) {
- int var41 = var39.length;
-
- while(var41 > 0) {
- --var41;
- var33 = new File(var39[var41]);
- var33.delete();
- }
- }
- }
-
- }
-
- return var5;
- }
-
- public Object call(Object var1, String var2, Object[] var3) throws BSFException {
- Object var4 = null;
-
- try {
- if (this.rexxclass != null) {
- Class[] var5 = new Class[var3.length];
-
- for(int var9 = 0; var9 < var3.length; ++var9) {
- var5[var9] = var3[var9].getClass();
- }
-
- Method var7 = ReflectionUtils.getMethod(this.rexxclass, var2, var5);
- var4 = var7.invoke((Object)null, var3);
- } else {
- System.err.println("NetRexxEngine: ERROR: rexxclass==null!");
- }
-
- return var4;
- } catch (Exception var8) {
- ((Throwable)var8).printStackTrace();
- if (var8 instanceof InvocationTargetException) {
- Throwable var6 = ((InvocationTargetException)var8).getTargetException();
- var6.printStackTrace();
- }
-
- throw new BSFException(BSFException.REASON_IO_ERROR, ((Throwable)var8).getMessage(), var8);
- }
- }
-
- public void declareBean(BSFDeclaredBean var1) throws BSFException {
- }
-
- public void undeclareBean(BSFDeclaredBean var1) throws BSFException {
- }
- }
-